package com.sbc.security.handler;

import com.sbc.security.tooltiks.JwtTokenProvider;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @description: 登录成功处理类
 * @author: songbaicheng
 * @create: 2024-04-30 15:14
 **/
@Component
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class LoginSuccessHandler implements AuthenticationSuccessHandler {

    JwtTokenProvider jwtTokenProvider;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest,
                                        HttpServletResponse httpServletResponse,
                                        Authentication authentication)
            throws IOException, ServletException {

        String jwt = jwtTokenProvider.generateToken(authentication.getName());
        httpServletResponse.setHeader("token", jwt);
    }
}
